<?php
namespace catchAdmin\externalGoods\model;

use catchAdmin\basics\model\Official;
use catchAdmin\externalGoods\model\search\NewsSearch;
use catcher\base\CatchModel;
use think\Exception;
use think\facade\Db;

class News extends CatchModel
{
    use NewsSearch;

//    protected $type = [
//        'post_time'  =>  'timestamp',
//    ];

    protected $connection = 'mysql_wc';

    protected $name = 'articles_copy';
    protected $field = [
        'id',
        'source',
        'search_name',
        'title',
        'path',
        'cover_url',
        'content_text',
        'content_html',
        'post_time',
        'is_rec',
        'is_rec2',
        'cate_id',
        'cate_name',
        'created_at',
        'updated_at',
        'deleted_at',
        'voice_count',
        'voice_url',
        'voice_task_id',
        '_ext_field_1',
        'tags',
        'score',
        'publish',
        'publish_title',
        'is_jiangsu_zone',
        'driver',
        'is_history',
        'is_have_html'
    ];

    public static function onAfterRead($news)
    {
        $news->post_time_str = date("Y-m-d H:i:s" , $news->post_time);
    }

    public function getList($tenant_id = 0)
    {
        $where = [];
//        if($tenant_id !== 0){
//            $where[]=['tenant_id','=',$tenant_id];
//        }
//
        $par = \request()->param();
        if(!array_key_exists('field_order',$par)){
            $order = ['post_time'=>'desc'];
        }else{
            $order = $par['field_order'];
        }

        return $this
            ->withoutField('updated_at,deleted_at,content_html')
            ->catchSearch()
            ->where($where)
//            ->order($this->aliasField('id'), 'desc')
            ->order($order)
            ->paginate()
            ->each(function (&$item) {
                $item['post_time'] = date("Y-m-d H:i:s" , $item['post_time']);
                //不为空
                if(!empty($item['tags'])){
                    $item['tags'] = explode(',',$item['tags']);
                }else{
                    $item['tags'] = [];
                }

                //判断发布标题是否为空
                if(empty($item['publish_title'])){
                    $item['publish_title'] = $item['title'];
                }
            });
    }

}
